package com.huqing.icu.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 */
public class RegexUtil {

    /**
     *
     * @param expressionStr 表达式
     * @param matcherStr  待匹配的字符串
     * @return 如果能匹配成功，返回第一个匹配成功的字符串。
     */
    public static String find(String expressionStr, String matcherStr) {
        Pattern pattern = Pattern.compile(expressionStr, Pattern.DOTALL); //包含行模式
        Matcher matcher = pattern.matcher(matcherStr);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    /**
     *
     * @param expressionStr
     * @param matcherStr
     * @param groupIndex
     * @return 如果能匹配成功，返回第groupIndex个匹配成功的字符串。
     */
    public static String find(String expressionStr, String matcherStr,
                              int groupIndex) {
        Pattern pattern = Pattern.compile(expressionStr);
        Matcher matcher = pattern.matcher(matcherStr);
        if (matcher.find()) {
            return matcher.group(groupIndex);
        }

        return null;
    }

    public static boolean isContainsEmoji(String source) {
        if (StringUtils.isNotBlank(source)) {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find())
                return true;
        }
        return false;
    }
}
